﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class ThongTinSanPham : Form
    {
        public string FName;
        public string tenanh;
        public string MSSP;
        public ThongTinSanPham()
        {
            InitializeComponent();
        }

        private void ThongTinSanPham_Load(object sender, EventArgs e)
        {
            LoadDataGridView1();
            comboMSLSP();
            comboMSNV();
        }
        private void LoadDataGridView1()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select MaSo_SP,Ten_SP,MoTa_SP,DonViTinh,GiaBan,HinhAnh_SP,Ten_LSP,HoTen_NV from SanPham join LoaiSanPham on SanPham.MaSo_LSP=LoaiSanPham.MaSo_LSP join NhanVien on SanPham.MaSo_NV=NhanVien.MaSo_NV ";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();

            DataSet ds = new DataSet();
            adapter.Fill(ds, "SanPham");
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ds.Tables["SanPham"];
            conn.Close();
        }

        private void buttonAddLSP_Click(object sender, EventArgs e)
        {
            ThemSP();
            LoadDataGridView1();

        }
        private void ThemSP()
        {
            if ( txtTSP.Text.Length == 0 || txtDVT.Text.Length == 0 || txtGB.Text.Length == 0)
                TBTCongSP.Text = "Thêm mới sản phẩm không thành công";
            else
            {
                Config file = new Config(Application.StartupPath + "\\Config.ini");
                string connectionstring = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "insert into SanPham(MaSo_SP,Ten_SP,MoTa_SP,DonViTinh,TonKho,GiaBan,HinhAnh_SP,MaSo_LSP,MaSo_NV) values ('',@Ten_SP,@MoTa_SP,@DonViTinh,@TonKho,@GiaBan,@HinhAnh_SP,@MaSo_LSP,@MaSo_NV)";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.Parameters.AddWithValue("@Ten_SP", txtTSP.Text);
                cmd.Parameters.AddWithValue("@MoTa_SP", txtMTSP.Text);
                cmd.Parameters.AddWithValue("@DonViTinh", txtDVT.Text);
                cmd.Parameters.AddWithValue("@TonKho", txtTK.Text);
                cmd.Parameters.AddWithValue("@GiaBan", txtGB.Text);
                cmd.Parameters.AddWithValue("@HinhAnh_SP", tenanh);
                cmd.Parameters.AddWithValue("@MaSo_LSP", comMSLSP.SelectedValue);
                cmd.Parameters.AddWithValue("@MaSo_NV", comMSNV.SelectedValue);
                cmd.ExecuteNonQuery();
                conn.Close();
                TBTCongSP.Text = "Thêm mới sản phẩm thành công";
            }
        }


        private void buttonAddHinh_Click(object sender, EventArgs e)
        {
            OpenFileDialog f = new OpenFileDialog();
            f.InitialDirectory = @"D:\9.LIBRARY DOCUMENTS\NAM II\VATC_HK III\THIẾT KẾ WEB\THUYET TRINH WEB\Nhom M3T - Lop 5103VIP-Finish\images";
            f.Filter = "Images Files (*.bmp, .*jpg, *.png)|*.bmp; *.jpg; *.png|All Files (*.*)|*.*";
            if (f.ShowDialog() == DialogResult.OK)
            {
                FName = f.FileName;
                tenanh=System.IO.Path.GetFileName(f.FileName);
                pictureBox1.Image = Image.FromFile(FName);
            }
        }

       

        private void comboMSLSP()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from LoaiSanPham";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();

            DataSet ds = new DataSet();
            adapter.Fill(ds, "LoaiSanPham");
            DataTable dt = ds.Tables["LoaiSanPham"];
            comMSLSP.DisplayMember = "Ten_LSP";
            comMSLSP.ValueMember = "MaSo_LSP";
            comMSLSP.DataSource = ds.Tables["LoaiSanPham"].DefaultView;
            conn.Close();
        }

        private void comboMSNV()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from NhanVien";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();

            DataSet ds = new DataSet();
            adapter.Fill(ds, "NhanVien");
            DataTable dt = ds.Tables["NhanVien"];
            comMSNV.DisplayMember = "HoTen_NV";
            comMSNV.ValueMember = "MaSo_NV";
            comMSNV.DataSource = ds.Tables["NhanVien"].DefaultView;
            conn.Close();
        }

        private void buttonExitLSP_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonUpdateLSP_Click(object sender, EventArgs e)
        {
           
            capnhatTTSP(MSSP);
            LoadDataGridView1();
        }
        private void capnhatTTSP(string MSSP)
        {
            if (txtTSP.Text.Length == 0 || txtDVT.Text.Length == 0 || txtGB.Text.Length == 0)
                TBTCongSP.Text = "Cập nhật sản phẩm không thành công";
            else
            {
                Config file = new Config(Application.StartupPath + "\\Config.ini");
                string connectionstring = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "update SanPham set Ten_SP=@Ten_SP,MoTa_SP=@MoTa_SP,DonViTinh=@DonViTinh,TonKho=@TonKho,GiaBan=@GiaBan,HinhAnh_SP=@HinhAnh_SP,MaSo_LSP=@MaSo_LSP,MaSo_NV=@MaSo_NV where MaSo_SP=@MaSo_SP";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.Parameters.AddWithValue("@MaSo_SP",MSSP);
                cmd.Parameters.AddWithValue("@Ten_SP", txtTSP.Text);
                cmd.Parameters.AddWithValue("@MoTa_SP", txtMTSP.Text);
                cmd.Parameters.AddWithValue("@DonViTinh", txtDVT.Text);
                cmd.Parameters.AddWithValue("@TonKho", txtTK.Text);
                cmd.Parameters.AddWithValue("@GiaBan", txtGB.Text);
                cmd.Parameters.AddWithValue("@HinhAnh_SP",tenanh);
                cmd.Parameters.AddWithValue("@MaSo_LSP", comMSLSP.SelectedValue);
                cmd.Parameters.AddWithValue("@MaSo_NV", comMSNV.SelectedValue);
                cmd.ExecuteNonQuery();
                conn.Close();
                TBTCongSP.Text = "Cập nhật sản phẩm thành công";
            }
        }

        private void buttonDelLSP_Click(object sender, EventArgs e)
        {
            deleteTTSP(MSSP);
            LoadDataGridView1();
        }
        private void deleteTTSP(string MSSP)
        {
            if ( txtTSP.Text.Length == 0 || txtDVT.Text.Length == 0 || txtGB.Text.Length == 0)
                TBTCongSP.Text = "Xóa sản phẩm không thành công";
            else
            {
                Config file = new Config(Application.StartupPath + "\\Config.ini");
                string connectionstring = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "delete from SanPham where MaSo_SP = @MaSo_SP";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.Parameters.AddWithValue("@MaSo_SP", MSSP);
                cmd.ExecuteNonQuery();
                conn.Close();
                TBTCongSP.Text = "Xóa sản phẩm thành công";
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
            MSSP = dataGridView1.Rows[e.RowIndex].Cells["MaSo_SP"].Value.ToString();
            loadTTSP(MSSP);
            
        }
        private void loadTTSP(string MSSP)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from SanPham where MaSo_SP = @MaSo_SP";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_SP", MSSP);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                
                txtTSP.Text = reader["Ten_SP"].ToString();
                txtMTSP.Text = reader["MoTa_SP"].ToString();
                txtDVT.Text = reader["DonViTinh"].ToString();
                txtTK.Text = reader["TonKho"].ToString();
                txtGB.Text = reader["GiaBan"].ToString();
                string duongdan = file.ReadValue("duongdanhinhanh","hinhanh");
                tenanh = reader["HinhAnh_SP"].ToString();
                pictureBox1.Image = Image.FromFile(duongdan+tenanh);
                comMSLSP.SelectedValue = reader["MaSo_LSP"].ToString();
                comMSNV.SelectedValue = reader["MaSo_NV"].ToString();
            }
            reader.Close();
            conn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            txtTSP.Text = null;
            txtMTSP.Text = null;
            txtDVT.Text = null;
            txtTK.Text = null;
            txtGB.Text = null;
            pictureBox1.Image = null;
            TBTCongSP.Text = null;
        }

        

        

      

       

       

       


        

       

        

        


    }
}
